CMScript is a Contextual Menu Plugin that runs AppleScripts on the "context".
CMScript is not going to be a separate product until the end of time. Plans are that it will be integrated with a future version of Leonard Rosenthol's "OSA Menu" (see <http://www.lazerware.com/~leonardr/>).
Installation
Put the two plugins, CMScript and ContextGrabber, and the "Contextual Menu Scripts" folder into the "Contextual Menu Items" folder inside the System Folder.
What it does
What does it do with the scripts in the "Contextual Menu Scripts" folder?
Wherever you are CMScript takes into account scripts found in two subfolders: "Universal Scripts" and "Application Name Scripts" (e.g. "Finder Scripts", "Internet Explorer 4.0 Scripts").
BTW, you can nest folders inside the script folders. The depth is arbitrarily restricted to three levels. Also you can use aliases to scripts instead of actual scripts.
When you activate the Contextual Menu by holding down the control key and clicking the mouse CMScript scans these folders for scripts. For each found script it checks if it applies to the context.
How it works (technically — skip if you like)
To facilitate finding out if a script applies to the context scripts must tell what kind of data they understand: the first line (or rather paragraph as it might be wrapped) of the script's description is used for this purpose. Put the type code of the data your script can operate on there. CMScript determines if the context can be made into the desired data type or a list of items of that type. For some types you can specify even more:
fss TEXTttxt - only SimpleText text documents
fss TEXT - any text document
fss ****ttxt - any SimpleText document
reco - any record
recopnme - a record containing a name (= «class pnam») property and possibly others.
recopnamldt - ditto + a date (= «class ldt ») property.
You can check for "object specifiers" (sorry for the jargon):
obj cfol - folders (only in the Finder)
And finally, of course, you can check for any old type, say text:
TEXT - anything that can be made into text
TEXT! - only text
If a script is applicable to the context its filename is put into the CM. When a script is chosen from the CM its "open"-handler gets called with the context data as a list (possibly having only one element) of items of the desired type.
How do I find out what the context is?
Now, the description above may have been just a bit too technical. Here's the easy way. :-)
Use the included "ContextGrabber" Module to find out about the context:
If caps lock is pressed during a context-click ContextGrabber does several things:
• It adds a menu item "Make Template Script". This command puts a script file on your desktop that's already preconfigured somewhat to be useful in the currently selected context. Remember to save it as a compiled script if you want to use it from the CMScript!
• It adds an item to "Copy Context to Clipboard" with the submenu items "Easy" and "Advanced".
"Advanced" is only suited for hardcore AppleEvent freaks, they'll get output from AEPrint (and understand what this means for them).
"Easy" is something suitable for use as a desired context (first line of the script's description) in your own scripts. One special case is that this plugin does not take file objects to be objects, i.e. "obj file", but treats them as file specifications. Thus you'd get the more helpful "fss TEXTttxt" if you selected a SimpleText text document.
Changing the order of scripts in the menu
You can determine the order of scripts in the contextual menu by following these conventions:
Generally scripts are alphabetically ordered, but if a script's or a folder's name starts with "xx)" (where "x" can anything except "(") this prefix is not shown in the menu.
Nested script folders whose names end with "-!!X" are ignored.
Files or folders whose name ends with "-***" generate a separator line in the context menu. CMScript tries to be smart about separators and avoids to add multiple adjacent ones. (By convention it is not possible to add a separator as the last item. Basically a plugin is allowed to separate itself from what comes before it, but not from what comes after it.)
Questions & Answers
Q. Why don't the installed plugins show up in the Contextual Menu?
A: The most common reason is that you tried to be smart some time before and disabled "SOMobjects™ for Mac OS". Don't do that! Contextual Menu Plugins don't work without it. And beside that it does nothing bad to your system: It is a so-called shared library that does absolutely nothing unless used by an application, extension, or plugin.
Q: Why do I see the Contextual Menu only in the Finder?
A: Well, the reason is that only very few applications actually support the Contextual Menu yet. But you can get around this by installing "Apple Data Detectors" (look for it on <http://applescript.apple.com>). Among other things this will give you the "Contextual Menu Enabler" extension that will enable the contextual menu in virtually any application.
A Note of Caution: If you use "Contextual Menu Enabler" 1.0.2 (retracted by Apple!) you'll find that it adds a menu item "Turn on Contextual Menus in app name " to the Help menu of some applications. These are applications that don't support the CM directly. If you turn on the CM in such an app be sure to quit and relaunch it before using the CM for the first time. This is not strictly necessary, but may avoid trouble in some cases.
Q: My script doesn't work. How do I find out what's happening?
A: The easiest way is to run the script inside your Script Editor.
Add the following lines to your script. Of course you need to replace "Finder" with the name of the application where you intend to use the script.
tell application "Finder" to set sel to the selection
open (sel)
From the "Controls" menu choose "Open Event Log" and enable both "Show Events" and "Show Event Results". This will give you a detailed log of what's happening in the script.
Unfortunately this method won't work with applications that do not have reasonable scripting support. And there may be cases where you have to test your script under the actual conditions of its use. But there's still hope:
Use the "Message Window" application to display information about your script's progress. (see <http://T3.acmetech.com/acme/files/freebies/MessageWin.hqx>)
Use the "show variables" command from the "programmer's tool" Scripting Addition to display the value of a variable in a dialog box. (see <http://www.scriptweb.com/osaxen/programmers_tool.html>)
Q: How do I find the actual class of the object(s) that was clicked?
A: Given that the application your context script is working in supports AppleScript sensibly do it like this:
• Make sure only one "Contextual Menu Extension" is installed. This can be a problem in particular on non-US systems as some software installers may install a duplicate US-copy on such a system (a case in point apparently is/was "Apple Data Detectors").
Please tell me about your problems so I can do something about them or add them here.
If you've written a cool script…
…please send me a copy. I'll consider it for inclusion with a future version of CMScript or maybe a separate "Service Pack".
Version History
The current version is always available from my home page (see below). Also, I'll submit new versions to the info-mac archive.
1.0.6 Improved ordering of scripts.
1.0.5 'obj ' context are now recognized correctly. (Thanks to Jens Miltner.)
Slight twiddling of memory handling.
1.0.4 Fixed a hierarchical menu handling bug. (Thanks to Lindsey Dubb for spotting it.)
1.0.3 Added "!" to specify strict checking for arbitrary types.
1.0.2 Added special treatment of record specifiers in context description.
1.0.1 First public version.
1.0 Not released.
Disclaimer
You might have guessed it already, but I take no responsibility for the consequences of anything CMScript does to your computer. If you think this is unacceptable, just don't use it.
That's it for now. I appreciate any bug reports and suggestions. Have as much fun with this thingie as possible.